﻿var lock = false;
var page = 0;
var returnUrl = "";
function PwdEditCheck() {
    if (!CheckPassword()) return false;
}
function CheckPassword() {
    if ($("#PasswordNew").val() != "") {
        if ($("#PasswordNew").val().length < 4) {
            CastMsg("至少输入4位密码");
            return false;
        }
    }
    if ($('#PasswordNew').val() != $('#NewPassword').val()) {
        CastMsg("两次输入密码不一致");
        $("#ConfirmInfo").css("color:red");
        return false;
    }

    return true;
}

function product_date() {
    $('.date').datetimepicker({
        format: 'Y-m-d H:m:s',
        timepicker: false,
    });
};
function ProductAddCheck() {
    if (!CheckBegin()) return false;
    if (!CheckEnd()) return false;
    if (!CheckTime()) return false;
    if (!CheckPrice()) return false;
    if (!CheckIntegration()) return false;
}
function ProductEditCheck() {
    if (!CheckBegin()) return false;
    if (!CheckEnd()) return false;
    if (!CheckTime()) return false;
    if (!CheckPrice()) return false;
    if (!CheckIntegration()) return false;
}
function CheckBegin() {
    if ($("#Begin").val() == "") {
        CastMsg("开始时间不能为空");
        console.log($("#Begin").val());
        return false;
    }
    return true;
}
function CheckEnd() {
    if ($("#End").val() == "") {
        CastMsg("结束时间不能为空");
        console.log($("#End").val());
        return false;
    }
    return true;
}
function CheckTime() {
    var startTime = $("#Begin").val();
    var start = new Date(startTime.replace("-", "/").replace("-", "/"));
    var endTime = $("#End").val();
    var end = new Date(endTime.replace("-", "/").replace("-", "/"));
    if (end < start) {
        console.log("time");
        CastMsg("时间选择不符合要求");
        return false;
    }
    return true;
}
function CheckPrice() {
    if ($("#Price").val() == "") {
        CastMsg("价格不能为空");
        console.log($("#Price").val());
        return false;
    }
    return true;
}
function CheckIntegration() {
    if ($("#Integration").val() == "") {
        CastMsg("积分不能为空");
        console.log($("#Integration").val());
        return false;
    }
    return true;
}
//加载产品
function LoadProduct(id, SID) {
    if (lock) {
        return;
    }
    else {
        lock = true;
        $(".loadMore").text("正在加载中，请稍后");
        $.ajax({
            url: "/Business/getProduct",
            type: "post",
            data: { "page": page, "id": id },
        }).done(function (model) {
            var str = "";
            var imgPath = "";
            var data = model.Data;
            if (model.Statu == "ok") {
                if (SID != "") {
                    for (var i = 0 ; i < data.length; i++) {
                        str += '<div class="product_item" id="product_item_' + data[i].ID + '"><a class="cover" href="/Business/ProductShow/' + data[i].ID + '" style="background-image: url(\'' + data[i].Cover + '\');"><p style="margin-top: 106px;" class="p_product"><span class="span10">产品名：' + data[i].ProductName + '</span></p><p class="p_product"><span class="span5">原价：' + data[i].Price + '</span><span class="span5">积分：' + data[i].Integration + '</span></p></a><div class="mangaer-list"><a href="/Business/ProductShow/' + data[i].ID + '"><span class="span4">查看详细</span></a><a href="javascript:deleteDialog(\'/Business/ProductDelete/' + data[i].ID + '?sid=' + SID + '\',\'product_item_' + data[i].ID + '\');"><span class="span3">删除</span></a><a href="/Business/ProductEdit/' + data[i].ID + '"><span class="span3">编辑</span></a></div></div>';
                    }
                }
                else {
                    for (var i = 0 ; i < data.length; i++) {
                        str += '<div class="product_item" id="' + data[i].ID + '"><a class="cover" href="/Product/Show/' + data[i].ID + '" style="background-image: url(\'' + data[i].Cover + '\');" target="_blank"><p style="margin-top: 114px;" class="p_product"><span class="span10">产品名：' + data[i].ProductName + '</span></p><p class="p_product"><span class="span5">原价：' + data[i].Price + '</span><span class="span5">积分：' + data[i].Integration + '</span></p></a></div>';
                    }
                }
                lock = false;
                page++;
                $(".lstProduct").append(str);
                $(".loadMore").text("下拉加载更多！");
            }
            else if (model.Statu == "end") {
                $(".loadMore").text("没有更多数据了！");
            }
            else if (model.Statu == "err") {
                $(".loadMore").text("网络异常");
            }
        });
    }
}

function BusinessEditSucess(data) {
    var business_id = $("#ID").val();
    if (data.Statu == "ok")
    {
        CastMsg("修改成功");
        setTimeout(function () {
            location.href = "/Business/Show/" + business_id;
        }, 2000);
    }
    else if(data.Statu=="err")
    {
        CastMsg("修改失败，请审核一下填写的信息");
    }
}
function BusinessEditFailure(data)
{
    CastMsg("登录信息失效，请重新登录");
    setTimeout(function () {
        location.href = "/Login?returnUrl=" + returnUrl;
    }, 2000);
}
function Load() {
    if ($(".lstProduct").length > 0) {
        LoadProduct(business_id, SID);
    }
}

$(document).ready(function () {
    Load();
    returnUrl = $("#returnUrl").val();
    $(window).scroll(
        function () {
            totalheight = parseFloat($(window).height())
               + parseFloat($(window).scrollTop());
            if (($(document).height() <= totalheight) && (typeof (business_id) != "undefined")) {
                LoadProduct(business_id, SID);
            }
        });
});


//兑换商品
function postChange(id,sid) {
    var url = "/Business/ProductExchange/" + id+"?sid="+sid;
    $.post(url, function (data) {
        if (data == 'ok' || data == 'OK')
            popMsg('兑换成功');
        else
            popMsg("兑换失败");
        closeDialog();
    });
}


function changeDialog(id,sid) {
    var html = '<div class="dialog">' +
        '<h3 class="dialog-title">提示</h3>' +
        '<p>您确认要兑换这个商品？</p>' +
        '<div class="dialog-buttons"><a href="javascript:postChange(\'' + id + '\',\'' + sid + '\')" class="button red">兑换</a> <a href="javascript:closeDialog()" class="button blue">取消</a></div>' +
        '</div>';
    var dom = $(html);
    dom.css('margin-left', -(dom.outerWidth() / 2));
    $('body').append(dom);
    setTimeout(function () { dom.addClass('active'); }, 10);
}


